#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <dirent.h>
#include <fcntl.h>

struct message
{
    char cmd[10];
    char filename[20];
};

void list_file(int , struct message *);
void get_file(int , struct message *);
void put_file(int , struct message *);

int main(int argc, const char *argv[])
{
    int listen_fd, socket_fd;
    struct sockaddr_in server_addr, client_addr;
    int client_addrlen;
    struct message msg;

    if (argc < 3)
    {
        fprintf(stderr, "usages : %s ip port\n", argv[0]);
        exit(-1);
    }

    if ((listen_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
    {
        perror("failed to create socket");
        exit(-1);
    }

    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(atoi(argv[2]));
    server_addr.sin_addr.s_addr = inet_addr(argv[1]);

    client_addrlen = sizeof(struct sockaddr);

    if (bind(listen_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0)
    {
        perror("failed to bind");
        exit(-1);
    }

    listen(listen_fd, 5);

    while(1)
    {
        if ((socket_fd = accept(listen_fd, (struct sockaddr *)&client_addr, &client_addrlen)) < 0)
        {
            perror("failed to accept");
            exit(-1);
        }

        memset(&msg, 0, sizeof(msg));
        recv(socket_fd, &msg, sizeof(msg), 0);

        printf("cmd : %s\n", msg.cmd);

        if (strcmp(msg.cmd, "list") == 0)
            list_file(socket_fd, &msg);

        if (strcmp(msg.cmd, "get") == 0)
            get_file(socket_fd, &msg);

        if (strcmp(msg.cmd, "put") == 0)
            put_file(socket_fd, &msg);
    }

    return 0;
}

void list_file(int socket_fd, struct message *msg)
{
    DIR *dir;
    struct dirent *dp;
    char buf[100];

    if ((dir = opendir(".")) < 0)
    {
        perror("failed to open dir");
        exit(-1);
    }

    memset(buf, 0, sizeof(buf));

    while ((dp = readdir(dir)) != NULL)
    {
        if (strcmp(dp->d_name, ".") == 0 || strcmp(dp->d_name, "..") == 0)
            continue;

        strcat(buf, dp->d_name);
        strcat(buf, " ");
    }

    send(socket_fd, buf, strlen(buf) + 1, 0);
    close(socket_fd);
    
    return ;
}

void get_file(int socket_fd, struct message *msg)
{
    int fd;
    char buf[1024];
    int num = 0;

    if ((fd = open(msg->filename, O_RDONLY)) < 0)
    {
        perror("failed to open file");
        exit(-1);
    }

    memset(buf, 0, sizeof(buf));

    while((num = read(fd, buf, sizeof(buf))) > 0)
    {
        usleep(500);
        send(socket_fd, buf, num, 0);
    }

    close(fd);
    close(socket_fd);

    return ;
}

void put_file(int socket_fd, struct message *msg)
{
    int fd;
    char buf[1024];
    int num = 0;

    if ((fd = open(msg->filename, O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 0)
    {
        perror("failed to create file");
        exit(-1);
    }

    memset(buf, 0, sizeof(buf));

    while((num = recv(socket_fd, buf, sizeof(buf), 0)) > 0)
        write(fd, buf, num);

    close(fd);
    close(socket_fd);
    return ;
}
